#include <iostream>
using namespace std;
#include <mysql/mysql.h>
#include <iomanip>

void demoLogic(MYSQL * sqlDB)
{
    //创建数据库表
    const char * sql = "create table if not exists students(id int auto_increment primary key,name text not null,age int,grade varchar(10));";

    int ret = mysql_query(sqlDB,sql);
    if(ret != 0)
    {
        cout<<"mysql_query error:"<<mysql_error(sqlDB)<<endl;
        exit(-1);
    }
    cout<<"创建数据表成功"<<endl;

    // sql = "update students set grade = 'A' where name = 'zhangsan'";
    // ret = mysql_query(sqlDB,sql);
    // if(ret != 0)
    // {
    //     cout<<"mysql_query error:"<<mysql_error(sqlDB)<<endl;
    //     exit(-1);
    // }

    //查询语句
    sql = "select * from students;";
    ret = mysql_query(sqlDB,sql);
    if(ret != 0)
    {
        cout<<"mysql_query error:"<<mysql_error(sqlDB)<<endl;
        exit(-1);
    }
    cout<<"查询成功"<<endl;

    //结果集
    MYSQL_RES * result =  mysql_store_result(sqlDB);
    if(result == NULL)
    {
        cout<<"mysql_store_result error:"<<mysql_error(sqlDB)<<endl;
        exit(-1);
    }

    unsigned int cols = mysql_num_fields(result);
    cout<<"cols:"<<cols<<endl;
    
    unsigned int rows = mysql_num_rows(result);
    cout<<"rows:"<<rows<<endl;
    //获取数据表的字段
    MYSQL_FIELD* field = NULL;
    while ((field = mysql_fetch_field(result)) != NULL)
    {
        cout<<left<<setw(10)<<field->name;
    }
    cout<< endl;

    //获取数据
    MYSQL_ROW row;
    while ((row = mysql_fetch_row(result)) != NULL)
    {
        //每个row变量表示一行
        for(int col = 0;col < cols;col++)
        {
            cout<<left<<setw(10)<<row[col];
        }
        cout<<endl;
    }
    cout<<endl;

    //释放结果集
    mysql_free_result(result);
}
int main()
{   
    MYSQL * sqlDB = nullptr;
    sqlDB = mysql_init(NULL);

    if(sqlDB == nullptr)
    {
        cout<<"mysql_init failed."<<endl;
        exit(-1);
    }

    cout<<"初始化数据库成功..."<<endl;

    //连接数据库
    const char * host = "localhost";
    const char * user = "root";
    const char * passwd = "1";
    const char * baseName = "test";
    int port = 3306;
    if(mysql_real_connect(sqlDB,host,user,passwd,baseName,port,NULL,0) == NULL)
    {   
        cout<<"file:"<<__FILE__<<"line:"<<__LINE__<<"connect mysql server error:"<<mysql_error(sqlDB)<<"SUCESS!"<<endl;
    }
    cout<<"连接成功"<<endl;

    //事务的开始
    if(mysql_query(sqlDB,"START TRANSACTION"))
    {
        cout<<"START TRANSACTION"<<mysql_error(sqlDB)<<endl;
        exit(-1);
    }

    //执行sql
    const char * sql1 = "update students set age = age - 100 where name = 'lisi';";
    const char * sql2 = "update students set age = age + 100 where name = 'wangwu';";

    if(mysql_query(sqlDB,sql1))
    {
        cout<<"file:"<<__FILE__<<"line:"<<__LINE__<<mysql_error(sqlDB)<<endl;
        mysql_query(sqlDB,"ROLLBACK");
        exit(-1);
    }
    else
    {
        unsigned int affectRows = mysql_affected_rows(sqlDB);
        cout<<"affectRows:"<<affectRows<<endl;
        if(affectRows == 0)
        {
            cout<<"未能修改"<<endl;
        }
        else
        {
        cout<<"修改成功"<<endl;
        }
    }

    if(mysql_query(sqlDB,sql2))
    {
        cout<<"file:"<<__FILE__<<"line:"<<__LINE__<<mysql_error(sqlDB)<<endl;
        mysql_query(sqlDB,"ROLLBACK");
        exit(-1);
    }
    else
    {
        unsigned int affectRows = mysql_affected_rows(sqlDB);
        cout<<"affectRows:"<<affectRows<<endl;
        if(affectRows == 0)
        {
            cout<<"未能修改"<<endl;
        }
        else
        {
        cout<<"修改成功"<<endl;
        }
    }
    //事务的提交
    if(mysql_query(sqlDB,"commit"))
    {
        cout<<"file:"<<__FILE__<<"line:"<<__LINE__<<mysql_error(sqlDB)<<endl;
        mysql_query(sqlDB,"ROLLBACK");
        exit(-1);
    }

    // //
    // if(mysql_affected_rows(sqlDB) == 0)
    // {
    //     cout<<"未能修改"<<endl;
    // }
    // else
    // {
    //     cout<<"修改成功"<<endl;
    // }
    //关闭数据库
    mysql_close(sqlDB);
    return 0;
}